package com.sgj.gulimall.ware.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @Author: Guoji Shen
 * @Date: 2021/7/9 8:59
 */
@EnableTransactionManagement
@MapperScan("com.sgj.gulimall.ware.dao")
@Configuration
public class WareMyBatisConfig {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    // 引入分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    // @Bean
    // public DataSource dataSource() {
    //     HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    //     if (StringUtils.hasText(dataSourceProperties.getName())) {
    //         dataSource.setPoolName(dataSourceProperties.getName());
    //     }
    //     return new DataSourceProxy(dataSource);
    // }
}